package com.jixiantubu.luntan;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.example.jixiantubu.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jixiantubu.adapter.TieziShouyeAdapter;
import com.jixiantubu.bean.PostInfoBean;
import com.jixiantubu.util.CustomProgressDialog;
import com.jixiantubu.util.MyApplication;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;

public class JingPinFragment extends Fragment {

	private PullToRefreshListView listView;
	TieziShouyeAdapter adapter;
	HttpUtils httpUtils;
	ImageView imageView;
	View view;
	List<PostInfoBean> list = new ArrayList<PostInfoBean>();
	String url = "http://" + MyApplication.getIP() + ":8080/Jinnang/post_servlet";
	BitmapUtils bitmapUtils;
	Context context;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		view = inflater.inflate(R.layout.jingpin, null);
		initViews();
		ViewUtils.inject(view);
		bitmapUtils = new BitmapUtils(getActivity());
		initData(url);
		return view;
	}

	private void initViews() {
		listView = (PullToRefreshListView) view.findViewById(R.id.post_jingpin_listview);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(getActivity(),
						Tiezi_Info_Activity.class);
				intent.putExtra("position", list.get(position-1));
				startActivity(intent);
			}
		});
	}

	private void initData(String url) {
		final CustomProgressDialog pd = new CustomProgressDialog(getActivity(),
				"正在拼命加载数据,请稍等！", R.anim.frame);
		pd.show();
		adapter = new TieziShouyeAdapter(getActivity(), list);
		listView.setAdapter(adapter);
		httpUtils = new HttpUtils();
		httpUtils.configSoTimeout(5000);
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("key", "3");
		params.addQueryStringParameter("page", "1");
		httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						pd.dismiss();
						String result = responseInfo.result;
						Gson gson = new Gson();
						Type typeOfT = new TypeToken<List<PostInfoBean>>() {
						}.getType();
						list = gson.fromJson(result, typeOfT);
						adapter.setList(list);
						adapter.notifyDataSetChanged();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						MyApplication.show(getActivity(), "加载数据失败，请检查网络!");
					}
				});
	}
}
	


